package com.hk.housekeeping.controller;

import com.hk.housekeeping.entity.UserEntity;
import com.hk.housekeeping.service.UserService;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     *添加用户信息
     * @param userEntity
     * @return 返回1代表添加成功605代表失败
     */
    @PostMapping("/add")
    public ResponseEntity<Integer> addUser(@RequestBody UserEntity userEntity){
        ResponseEntity<Integer> entity=null;
        try {
            entity = userService.addUser(userEntity);
        }catch (Exception e){
            entity=ResponseEntity.error(ResponseCode.USER_EDIT_ERROR);
        }
        return entity;
    }

    /**
     * (假)删除用户信息
     * @param uid
     * @return 返回1代表添加成功605代表失败
     */
    @GetMapping("/delete/{uid}")
    public ResponseEntity<Integer> delUser(@PathVariable int uid){
        ResponseEntity<Integer> entity=null;
        try {
            entity = userService.delUser(uid);
        }catch (Exception e){
            entity=ResponseEntity.error(ResponseCode.USER_EDIT_ERROR);
        }
        return entity;
    }

    /**
     * 修改用户信息
     * @param userEntity
     * @return 返回1代表添加成功605代表失败
     */
    @PostMapping("/modify")
    public ResponseEntity<Integer> modifyUser(@RequestBody UserEntity userEntity){
        ResponseEntity<Integer> entity=null;
        try {
            entity = userService.updateUser(userEntity);
        }catch (Exception e){
            entity=ResponseEntity.error(ResponseCode.USER_EDIT_ERROR);
        }
        return entity;
    }

    /**
     * 通过id查询用户信息【信息回显】
     * @param uid
     * @return  用户信息
     */
    @GetMapping("/get/{uid}")
    public UserEntity getUser(@PathVariable int uid){
        return userService.getUser(uid);
    };

    /**
     * 通过昵称或手机号查询
     * @param fuzzy
     * @return 用户信息
     */
    @GetMapping("/getName/{fuzzy}")
    public List<UserEntity> getUserName(@PathVariable String fuzzy){
        return userService.getUserFuzzy(fuzzy);
    }

    /**
     * 通过注册时间查询
     * @param datetime1
     * @param datetime2
     * @return 用户信息
     */
    @GetMapping("/getTime/{datetime1}/{datetime2}")
    public List<UserEntity> getUserTime(@PathVariable String datetime1,@PathVariable String datetime2){
        return userService.getUserTime(datetime1,datetime2);
    }

    /**
     * 分页
     * @param offset
     * @param limit
     * @return 分页对象
     */
    @GetMapping("/page/{offset}/{limit}")
    public ResponseEntity<PageUtils<List<UserEntity>>> getUserPage(@PathVariable int offset, @PathVariable int limit){
        return userService.getSelAll(offset, limit);
    }

}
